package com.example.doctor.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.common.entity.DoctorBaseInfo;
import com.example.common.entity.HDoctorNotice;
import com.example.common.support.ApiException;
import com.example.doctor.dto.req.NoticeListReq;
import com.example.doctor.dto.req.WcJsapiTicket;

import com.example.doctor.dto.resp.HDoctorNoticeRsp;
import com.example.doctor.dto.resp.TodayCount;
import com.example.doctor.dto.resp.TodayDateRsp;
import com.example.doctor.jpa.HDoctorNoticeRepository;
import com.example.doctor.jpa.IndexRepository;
import com.example.doctor.mapper.IndexMapper;
import com.example.doctor.service.IndexService;

import com.example.doctor.utils.UserUtils;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author sifan
 * @PackagName:com.example.doctor.service.impl
 * @ClassName: IndexServiceImpl
 * @Description:
 * @date:2021/1/19 13:30
 */
@Service
public class IndexServiceImpl implements IndexService {


    @Resource
    IndexRepository indexRepository;

    @Resource
    UserUtils userUtils;

    @Resource
    IndexMapper indexMapper;

    @Value("${appID}")
    private String appID;

    @Value("${appsecret}")
    private String appsecret;


    @Resource
    HDoctorNoticeRepository hDoctorNoticeRepository;

    @Override
    public TodayDateRsp TodayDate() {
        DoctorBaseInfo user = userUtils.getUser();
        Long dcId = user.getDcId();
        HDoctorNotice doctorNotice = hDoctorNoticeRepository.findByLastTop();

        //统计
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        //在线复诊
        Integer onLineCount = indexMapper.onLineCount(dcId, 10, dateTime);
        //待接诊
        Integer toonLineCount = indexMapper.toonLineCount(dcId, 10, dateTime);
        TodayCount onLineCountD = new TodayCount();
        onLineCountD.setIsOpen(true);
        onLineCountD.setInquiryName("在线复诊");
        onLineCountD.setTodayCount(onLineCount);
        onLineCountD.setToBeCount(toonLineCount);

        //图文问诊
        Integer imagCount = indexMapper.onLineCount(dcId, 11, dateTime);
        //待接诊
        Integer toimageCount = indexMapper.toonLineCount(dcId, 11, dateTime);

        TodayCount imagCountD = new TodayCount();
        imagCountD.setIsOpen(true);
        imagCountD.setInquiryName("图文问诊");
        imagCountD.setTodayCount(imagCount);
        imagCountD.setToBeCount(toimageCount);


        //电话问诊
        Integer phoneCount = indexMapper.onLineCount(dcId, 12, dateTime);
        //待接诊
        Integer tophoneCount = indexMapper.toonLineCount(dcId, 12, dateTime);

        TodayCount phoneCountD = new TodayCount();
        phoneCountD.setIsOpen(true);
        phoneCountD.setInquiryName("电话问诊");
        phoneCountD.setTodayCount(phoneCount);
        phoneCountD.setToBeCount(tophoneCount);


        //视频问诊
        Integer videoCount = indexMapper.onLineCount(dcId, 13, dateTime);
        //待接诊
        Integer tovideoCount = indexMapper.toonLineCount(dcId, 13, dateTime);

        TodayCount videoCountCountD = new TodayCount();
        videoCountCountD.setIsOpen(true);
        videoCountCountD.setInquiryName("视频问诊");
        videoCountCountD.setTodayCount(videoCount);
        videoCountCountD.setToBeCount(tovideoCount);


        TodayDateRsp todayDateRsp = new TodayDateRsp();
        todayDateRsp.setHead(user.getHead());
        if (!ObjectUtils.isEmpty(doctorNotice)) {
            todayDateRsp.setNotice(doctorNotice.getContents());
        }

        List<TodayCount> todayCountList = new ArrayList<>();
        todayCountList.add(onLineCountD);
        todayCountList.add(imagCountD);
        todayCountList.add(phoneCountD);
        todayCountList.add(videoCountCountD);
        todayDateRsp.setDate(todayCountList);
        return todayDateRsp;
    }

    @Override
    public Map<String,Object> NoticeList(NoticeListReq noticeListReq) {
        Map<String, Object> result = new HashMap<>();
        List<HDoctorNoticeRsp> doctorNoticeRsps = new ArrayList<>();
        long count = indexRepository.countNoticeList();
        List<HDoctorNotice> noticeList = indexRepository.noticeList((noticeListReq.getPage() - 1) * noticeListReq.getLimit(), noticeListReq.getLimit());
        for (HDoctorNotice doctorNotice : noticeList) {
            HDoctorNoticeRsp doctorNoticeRsp = new HDoctorNoticeRsp();
            doctorNoticeRsp.setContents(doctorNotice.getContents());
            doctorNoticeRsp.setCreateTime(doctorNotice.getCreateTime());
            doctorNoticeRsps.add(doctorNoticeRsp);
        }
        result.put("total",count);
        result.put("list",doctorNoticeRsps);
        return result;
    }


    @Override
    public WxJsapiSignature WcJsapiTicket(WcJsapiTicket wcJsapiTicket) {

        WxMpService wxMpService = new WxMpServiceImpl();
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(appID);//TODO
        configStorage.setSecret(appsecret);//TODO
        wxMpService.setWxMpConfigStorage(configStorage);
        try {
            WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(wcJsapiTicket.getUrl());
            return jsapiSignature;
        } catch (Exception e) {
            throw new ApiException(400, "授权失败");
        }
    }
}
